/*
 * Menu.cpp
 *
 *  Created on: Sep 11, 2012
 *      Author: dimir
 */

#include "Menu.h"

using namespace std;

MenuItem
menu(){
	int ret = -1;
	while ( (ret < I_REGISTRATION) || ( ret > I_EXIT)){
		for (int i = I_REGISTRATION;i <= I_EXIT; i++){
			cout << i << ":" << MSG[i] << endl;
		}
		cout << "Select number of item of menu:" << endl;
		cin >> ret;
	}
	return (MenuItem)ret;
}

info*
process(MenuItem item){
	info* inf =(info *) malloc(sizeof(info));
	string str;
	inf->item = item;
	switch (item) {
		case I_REGISTRATION:
			cout << "Enter id of client for registraion:" << endl;
			cin >> inf->fromID;
			break;
		case I_SEND_MESSAGE:
			cout << "Enter from id:" << endl;
			cin >> inf->fromID;
			cout << "Enter to id:" << endl;
			cin >> inf->toID;
			cout << "Enter message:" << endl;
			getc(stdin);
			getline(cin , str);
			dbg_msg(LOG_DEBUG, "User enter new message:");
			dbg_msg(LOG_DEBUG, str.c_str());
			inf->message =(char *) malloc(str.length() + 1);
			inf->message = strcpy(inf->message, str.c_str());
			break;
		case I_READ_MESSAGE:
			cout << "Enter from id:" << endl;
			cin >> inf->fromID;
			break;
		case I_GET_ID_CLIENTS:
			cout << "Enter from id:" << endl;
			cin >> inf->fromID;
			break;
		case I_GET_ID_CLIENTS_LOCAL:

			break;
		case I_UNREGISTERED:
			cout << "Enter from id:" << endl;
			cin >> inf->fromID;
			break;
		case I_EXIT:

			break;
		default:
			dbg_msg(LOG_DEBUG, "Invalid value of item in function process from Menu.h");
			free(inf);
			inf = NULL;
			break;
	}
	return inf;
}
Client* searchClient(vector<Client*>& clients, u_int32_t id){
	char msg_error[128];
	int n_clients = clients.size();
	for(int i = 0; i < n_clients; i++){
		if (clients[i] != NULL){
			if (clients[i]->getId() == id){
				return clients[i];
			}
		}
	}
	sprintf(msg_error, "Error client with id: %u is not exist.", id);
	dbg_msg(LOG_ERROR, msg_error);
	return NULL;
};

void
action(const info* inf, vector<Client*>& clients){
	Message* msg;
	Client* client;
	if (inf == NULL) {
		dbg_msg(LOG_DEBUG, "In function 'action': inf = NULL");
		return ;
	}
	switch (inf->item) {
		case I_REGISTRATION:
			client = searchClient(clients, inf->fromID);
			if (client != NULL){
				client->registration();
			}else
				dbg_msg(LOG_ERROR, "Error in registration of client.");
			break;
		case I_SEND_MESSAGE:
			msg = genMsg(inf->fromID, inf->toID, inf->message);
			client = searchClient(clients, inf->fromID);
			if (client != NULL){
				client->send(*msg);
				if (msg != NULL){
					delete msg;
				}
			}else
				dbg_msg(LOG_ERROR, "Error in sending message.");
			break;
		case I_READ_MESSAGE:
			client = searchClient(clients, inf->fromID);
			if (client != NULL){
				if (client->hasNewMessage()){
					msg = client->readMessage();
					cout  << *msg;
					if (msg != NULL){
						delete msg;
					}

				}else{
					cout << "Client#" << inf->fromID << " not have a new message" << endl;
				}
			}
			break;
		case I_GET_ID_CLIENTS:
			msg = genReqGetClients(inf->fromID);
			client = searchClient(clients, inf->fromID);
			if (client != NULL){
				client->send(*msg);
				if(msg != NULL)
					delete msg;
			}else
				dbg_msg(LOG_ERROR, "Error in request 'get clients'.");
			break;
		case I_GET_ID_CLIENTS_LOCAL:
			cout << "All local clients:" << endl;
			for (size_t i = 0; i < clients.size(); i++){
				cout << "client#" << i << " id : " << clients[i]->getId() << endl;
			}
			break;
		case I_UNREGISTERED:
			client = searchClient(clients, inf->fromID);
			if (client != NULL){
				client->unregistration();
			}else
				dbg_msg(LOG_ERROR, "Error in request 'unregistered'.");
			break;
		case I_EXIT:
				dbg_msg(LOG_TRACE, "Exit from the programm");
				for (size_t i= 0; i < clients.size(); i++)
					delete clients[i];
				exit(0);
			break;
		default:
			dbg_msg(LOG_DEBUG, "Invalid value of inf->item in function action from Menu.h");
			break;
	}
	return ;
}
